home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: [QE] : file open problem
- Date: 7 Apr 96 18:54:20 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.828903260@rscernix>
- References: <4k8mja$ddn@news.kreonet.re.kr>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4k8mja$ddn@news.kreonet.re.kr> Parao@kyebek.kjist.ac.kr (Don Kim) writes:
-
- >Hello, there
- >
- >I have a problem with file opening.
- >In my code,
- >
- > saveTextToFile ( char *text, char *filename) {
- > FILE *file;
- >
- > if ( ( file = fopen (filename, "w") = = NULL )
- ^^^
- 1. This is a syntax error in C.
-
- 2. The parentheses don't match.
-
- > cout << "Cannot open file" << endl;
-
- This line is completely meaningless in C. The << operator doesn't
- accept pointer operands (and it's not clear from your code what is the
- type of cout and endl :-)
-
- > else {
- > (void) fprintf (file, "%s\n", text);
- > (void) fclose(file);
- > }
- > }
- >
- > :
- > :
- >
- > char *_type = "part";
- > saveTextToFile ( _type, _type );
- >
- >Is anything wrong in my source code ? My program always displays "Cannot
- >open file"....
- >
- >Let me know how to fix the problem.
-
- 1. Write it in C (or post to c.l.c++ if you insist on using C++).
-
- 2. The line opening the file should look like this:
-
- if ((file = fopen(filename, "w")) == NULL)
-
- 3. To be on the safe side, avoid identifiers starting with an underscore.
- Sometimes they're legal, some other times they are in the implementation
- namespace and it isn't worth the effort to try to figure out whether
- they can be used in a certain context or not. They only create
- headaches without buying you anything at all.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-